﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Spinning.Spinning.Unitity;
using Microsoft.Xna.Framework;

namespace Spinning.Spinning.Entity
{
    class ContinuousSprite : Sprite
    {
        Camera _camera;
        Vector2 positionSec;
        int spriteNumber;
        List<Texture2D> textureList = new List<Texture2D>();
        List<Vector2> postionList = new List<Vector2>();


        public ContinuousSprite(Camera camera, int number)
        {
            this._camera = camera;
            Color = Microsoft.Xna.Framework.Color.White;
            Scale = new Microsoft.Xna.Framework.Vector2(1);
            Alpha = 1;
            positionSec = new Vector2();
            this.spriteNumber = number;
        }

        public override void OnLoad()
        {
            if (textureName.Length == 0)
                throw new Exception("textureName");
            ResourceManager.ContentRoot = "Images";
            for (int i = 0; i < spriteNumber; i++)
            {
                _texture = ResourceManager.LoadTexture2D(textureName+i);
                textureList.Add(_texture);
                postionList.Add(new Vector2());
            }
            _texture = ResourceManager.LoadTexture2D(textureName + "0");
            textureList.Add(_texture);
            postionList.Add(new Vector2());
        }

        int tempRange = -1, tempRange1 = 1;

        protected override void UpdateSelf(GameTime gameTime)
        {
             base.UpdateSelf(gameTime);
            if (_camera.Position.Y >= 0)
            {
                tempRange1 = 1;
                if ((int)(_camera.Position.Y / (_texture.Height * spriteNumber)) != tempRange)
                {
                    tempRange = (int)(_camera.Position.Y / (_texture.Height * spriteNumber));
                    for (int i = 0; i < postionList.Count; i++)
                        postionList[i] = new Vector2(postionList[i].X, (i * _texture.Height) + (_texture.Height * spriteNumber) * tempRange);
                    //positionSec = new Vector2(positionSec.X, ((int)(_camera.Position.Y / _texture.Height) + 1) * _texture.Height);
                }
            }
            else 
            {
                tempRange = -1;
                if ((int)(_camera.Position.Y / (_texture.Height * spriteNumber)) != tempRange1)
                {
                    tempRange1 = (int)(_camera.Position.Y / (_texture.Height * spriteNumber));
                    for (int i = 0; i < postionList.Count; i++)
                        postionList[i] = new Vector2(postionList[i].X, (-(postionList.Count - i - 1) * _texture.Height) + (_texture.Height * spriteNumber) * tempRange1);
                    //positionSec = new Vector2(positionSec.X, ((int)(_camera.Position.Y / _texture.Height) + 1) * _texture.Height);
                }
            }
            
        }

        protected override void DrawSelf(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.DrawSelf(spriteBatch, gameTime);
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Projection);
            for (int i = 0; i < textureList.Count; i++)
            {
                spriteBatch.Draw(textureList[i], postionList[i], null, this.Color, Rotation, Origin * new Vector2(_texture.Width, _texture.Height), Scale, SpriteEffects.None, 0);
            }
            //spriteBatch.Draw(_texture, positionSec, null, this.Color, Rotation, Origin * new Vector2(_texture.Width, _texture.Height), Scale, SpriteEffects.None, 0);
            spriteBatch.End();
        }
    }
}
